<template>
    <tool-header>
        {{ title }}
    </tool-header>
    <a-row justify="center">
        <a-col :flex="2">
            <div style="margin:20px">
                <a-form>
                    <a-form-item label="PEM Data">
                        <a-textarea v-model:value="modelRef.pemData" placeholder="" :rows="20" />
                    </a-form-item>
                </a-form>
                <a-button type="primary" @click="genPGP">PGP</a-button>
            </div>
        </a-col>
        <a-col :flex="2">
            <div style="margin:20px">
                <a-form :label-col="labelCol" :wrapper-col="wrapperCol">
                    <a-form-item label="Version" name="version">
                        <a-input v-model:value="certInfo.version"></a-input>
                    </a-form-item>
                    <a-form-item label="Subject" name="subject">
                        <a-textarea v-model:value="certInfo.subject" auto-size />
                    </a-form-item>
                    <a-form-item label="Issuer" name="issuer">
                        <a-textarea v-model:value="certInfo.issuer" auto-size />
                    </a-form-item>
                    <a-form-item label="Serial Number" name="sn">
                        <a-input v-model:value="certInfo.sn"></a-input>
                    </a-form-item>
                    <a-form-item label="Valid From" name="vFrom">
                        <a-input v-model:value="certInfo.vFrom"></a-input>
                    </a-form-item>
                    <a-form-item label="Valid End" name="vEnd">
                        <a-input :status="validDatestatus" v-model:value="certInfo.vEnd"></a-input>
                    </a-form-item>
                    <a-form-item label="SHA-1 Fingerprint" name="sha1fp">
                        <a-textarea v-model:value="certInfo.sha1fp" auto-size />
                    </a-form-item>
                    <a-form-item label="SHA-256 Fingerprint" name="sha256fp">
                        <a-textarea v-model:value="certInfo.sha256fp" auto-size />
                    </a-form-item>
                    <a-form-item label="MD5 Fingerprint" name="md5fp">
                        <a-textarea v-model:value="certInfo.md5fp" auto-size />
                    </a-form-item>
                </a-form>
            </div>
        </a-col>
    </a-row>
</template>
<script lang="ts" setup>
import { reactive, ref, computed } from 'vue';
import { useRoute } from 'vue-router'
import ToolHeader from '../ToolHeader.vue'
import * as forge from 'node-forge'
import dayjs from 'dayjs';
import * as openpgp from 'openpgp';

const route = useRoute();
const title = route.query.title
let validDatestatus = ''
const pemData = `-----BEGIN CERTIFICATE-----
MIIE/TCCAuWgAwIBA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42KnsBQ1ehyeIoWW5x8jg
RHkV1lNCnk4yp07PK1QJVaqLMUsC62/yMJCJFr8wq7vxT3Xzumh8cr4e9by6pqik
h0JDlYcqVwUP/XHAAaxDXdJpwl3hguvwPCmFnQDiZC+0ONLk+kbhpBBoFpZQZJ+5
yvte8FPBq2jVXnBjXRB/MvJIJ27Yfwnlf6tNkYX8f4wVU7ZpA7+ZkfcZ2rFhRspk
p3N1Q1WqxnugYPQNNXU+5OGboLRojbidEl5SZrFDFTqJ2arJSIPb5fNzqVVjr2mI
uDlpvFu2PLD/w4zE/1+lWOREDPIU8cHVZaRD+JS3CrOJebAylRl2eQ9m+FUsaYsx
BwwFl9C2E+0Vs7Zy+aJsYzksRJ06zzQnXStjzTlWlxhyHAwATLEiVtV+T4R8K6px
3XTsRFfvAkWujp9CZe2QMB0BoqQhqLk2Z4hi6bBE4TNlI+EQHxTJum5OPzwwoioK
/IcxBKas//ygP50TUdPQnYt9jQd0ifPTEtmNGda8ven/ZaBXlj6moIohj8NDhi5E
6MocatcGKMFpfjjwDBv3KTyL35nDujpSq6dvminX9G55NwVfXXamCLwVq3LwJ+oF
gPypQ/sMrDRyJsjChZyw7kV0CHC+OMgQLZMaH8boxy80
-----END CERTIFICATE-----
`
const labelCol = { span: 6 };
const wrapperCol = { span: 14 };
interface CertInfo {
    version: string,
    subject: string,
    issuer: string,
    sn: string,
    vFrom: string,
    vEnd: string,
    sha1fp: string,
    sha256fp: string,
    md5fp: string,
    pemData?: string
}
const modelRef = reactive<CertInfo>({
    pemData: pemData,
    version: '',
    subject: '',
    issuer: '',
    sn: '',
    vFrom: '',
    vEnd: '',
    sha1fp: '',
    sha256fp: '',
    md5fp: ''
});

const certInfo = computed(() => {
    let pem = modelRef.pemData == undefined ? '' : modelRef.pemData
    const cert = forge.pki.certificateFromPem(pem);
    const der = forge.asn1.toDer(forge.pki.certificateToAsn1(cert)).getBytes();
    for (let type of ['md5', 'sha1', 'sha256']) {
        let m = forge.md[type].create()
        m.update(der);
        let fingerprint = m.digest()?.toHex()?.match(/.{2}/g)?.join(':')
            .toUpperCase();
        modelRef[type + 'fp'] = <string>fingerprint;
    }
    modelRef.subject = cert.subject.attributes.map((item) => {
        return item.shortName + '=' + item.value
    }).join(' , ')
    modelRef.issuer = cert.issuer.attributes.map((item) => {
        return item.shortName + '=' + item.value
    }).join(' , ')
    modelRef.vFrom = dayjs(cert.validity.notBefore).format('YYYY-MM-DD HH:mm:ss')
    modelRef.vEnd = dayjs(cert.validity.notAfter).format('YYYY-MM-DD HH:mm:ss')
    if (dayjs().isAfter(dayjs(cert.validity.notAfter))) {
        validDatestatus = 'error'
        modelRef.vEnd = modelRef.vEnd + ' (expired)'
    } else {
        validDatestatus = ''
    }
    modelRef.sn = cert.serialNumber
    modelRef.version = cert.version.toString()
    return modelRef;
})
const genPGP = async () => {
    const { privateKey, publicKey } = await openpgp.generateKey({
        type: 'rsa', // Type of the key
        rsaBits: 2048, // RSA key size (defaults to 4096 bits)
        userIDs: [{ name: 'Jon Smith', email: 'jon@example.com' }], // you can pass multiple user IDs
        passphrase: 'super long and hard to guess secret' // protects the private key
    });

    console.log(privateKey);     // '-----BEGIN PGP PRIVATE KEY BLOCK ... '
    console.log(publicKey);      // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
}
</script>